﻿using HKB.Common;
using HKB.Web;
using HKB.Web.Admin;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml;

public partial class Admin_Admin_ShopIndexArticleAdd : Admin, IRequiresSessionState
{
    private Shop_Article _Article = null;
    private Shop_Class _class = null;
    private SpecialBll _special = null;
    private Shop_Module _module = null;
    private int m_ArticleID = 0;
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
    protected global_asax applicationInstance
    {
        get
        {
            return (global_asax)this.Context.ApplicationInstance;
        }
    }
    protected Shop_Article m_Article
    {
        get
        {
            Shop_Article article;
            if (this._Article == null)
            {
                this._Article = new Shop_Article(this.conn);
                article = this._Article;
            }
            else
            {
                article = this._Article;
            }
            return article;
        }
    }
    protected Shop_Class m_class
    {
        get
        {
            Shop_Class @class;
            if (this._class == null)
            {
                this._class = new Shop_Class(this.conn);
                @class = this._class;
            }
            else
            {
                @class = this._class;
            }
            return @class;
        }
    }
    protected SpecialBll m_special
    {
        get
        {
            SpecialBll special;
            if (this._special == null)
            {
                this._special = new SpecialBll(this.conn);
                special = this._special;
            }
            else
            {
                special = this._special;
            }
            return special;
        }
    }
    protected Shop_Module module
    {
        get
        {
            Shop_Module module;
            if (this._module == null)
            {
                this._module = new Shop_Module(this.conn);
                module = this._module;
            }
            else
            {
                module = this._module;
            }
            return module;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.admin.CheckAdminPower(10))
        {
            base.WriteLog("Modify", Language.GetAdminMsg("Log/Access", "0"), "Article");
            base.WriteRightErrMsg();
        }
        this.m_ArticleID = Fetch.GetID("ArticleID");
        if (!base.IsPostBack)
        {
            this.BandChannellist();
            this.BandClasslist();
            this.BandSpeciallist();
            this.Add_ArticleType.Attributes.Add("onchange", "selectArtitleType();");
            this.Add_LinkType.Attributes.Add("onclick", "selectLinkType();");
            this.Edit_ArticleType.Attributes.Add("onchange", "selectArtitleType();");
            this.Edit_LinkType.Attributes.Add("onclick", "selectLinkType();");
            this.Edit_InputTime.Attributes.Add("onclick", "SelectDate(" + this.Edit_InputTime.UniqueID + ",'yyyy-MM-dd');");
            this.Add_InputTime.Attributes.Add("onclick", "SelectDate(" + this.Add_InputTime.UniqueID + ",'yyyy-MM-dd');");
            if (Fetch.Get("Action") == "Edit")
            {
                this.BandEditTable();
            }
            else
            {
                this.BandAddTable();
            }
        }
    }
    protected void BandChannellist()
    {
        DataTable dataSource = this.module.GerActiveIdTitle();
        this.Edit_Channel.DataSource = dataSource;
        this.Edit_Channel.DataBind();
        this.Add_Channel.DataSource = dataSource;
        this.Add_Channel.DataBind();
    }
    protected void BandClasslist()
    {
        DataTable allClassNameFillDropDown = this.m_class.GetAllClassNameFillDropDown();
        this.Edit_ClassID.DataSource = allClassNameFillDropDown;
        this.Edit_ClassID.DataBind();
        this.Add_ClassID.DataSource = allClassNameFillDropDown;
        this.Add_ClassID.DataBind();
    }
    protected void BandSpeciallist()
    {
        DataTable allSpcilNameID = this.m_special.GetAllSpcilNameID();
        this.Edit_SpecialID.DataSource = allSpcilNameID;
        this.Edit_SpecialID.DataBind();
        this.Add_SpecialID.DataSource = allSpcilNameID;
        this.Add_SpecialID.DataBind();
    }
    protected void BandAddTable()
    {
        this.AddTable.Visible = true;
        this.EditTable.Visible = false;
        this.Edit_TemplatePath.Text = this.config["Index"].ToString();
        this.Add_InputTime.Text = DateTime.Now.ToString();
        this.Add_Hits.Text = "0";
        this.Add_Editor.Text = base.admin.AdminName;
        this.Add_Validay.Text = "7";
        if (!string.IsNullOrEmpty(Fetch.Get("ModuleID")))
        {
            this.Add_Channel.SelectedValue = Fetch.Get("ModuleID");
            this.ReturnBack.NavigateUrl = "Admin_ShopIndexArticle.aspx?ModuleID=" + Fetch.Get("ModuleID");
        }
    }
    protected void BandEditTable()
    {
        this.AddTable.Visible = false;
        this.EditTable.Visible = true;
        if (this.m_ArticleID == 0)
        {
            this.WriteErrMsg("", Language.GetAdminMsg("Common/ParamErr"), "");
        }
        DataTable articleByArticleID = this.m_Article.GetArticleByArticleID(this.m_ArticleID);
        if (articleByArticleID.Rows.Count <= 0)
        {
            this.WriteErrMsg("", Language.GetAdminMsg("Common/NotData"), "");
        }
        DataRow dataRow = articleByArticleID.Rows[0];
        this.Edit_ArticleID.Text = this.m_ArticleID.ToString();
        this.Edit_ShortTitle.Text = dataRow["ShortTitle"].ToString();
        this.Edit_LongTitle.Text = dataRow["LognTitle"].ToString();
        this.Edit_Channel.SelectedValue = dataRow["ChannelID"].ToString();
        this.Edit_ArticleType.Text = dataRow["ArticleType"].ToString();
        this.IndxeImg.ImageUrl = dataRow["UploadPicUrl"].ToString();
        this.Edit_IsHighDisplay.Text = dataRow["IsHighDisplay"].ToString();
        this.Edit_ClassID.SelectedValue = dataRow["ClassID"].ToString();
        this.Edit_Status.Text = dataRow["Status"].ToString();
        this.Edit_SpecialID.SelectedValue = dataRow["SpecialID"].ToString();
        this.Edit_IsTop.Checked = (dataRow["OnTop"].ToString() == "1");
        this.Edit_IsElite.Checked = (dataRow["Elite"].ToString() == "1");
        this.Edit_Editor.Text = dataRow["Editor"].ToString();
        this.Edit_Hits.Text = dataRow["Hits"].ToString();
        this.Edit_InputTime.Text = dataRow["InputTime"].ToString();
        this.Edit_Validay.Text = dataRow["Validay"].ToString();
        this.Edit_LinkType.Text = dataRow["LinkType"].ToString();
        this.Edit_LinkUrl.Text = dataRow["LinkUrl"].ToString();
        this.FCKeditor1.Text = dataRow["ArticleContent"].ToString();
        this.Edit_TemplatePath.Text = dataRow["TemplatePath"].ToString();
        if (!string.IsNullOrEmpty(Fetch.Get("ModuleID")))
        {
            this.Edit_Channel.SelectedValue = Fetch.Get("ModuleID");
            this.ReturnBack.NavigateUrl = "Admin_ShopIndexArticle.aspx?ModuleID=" + Fetch.Get("ModuleID");
        }
    }
    protected void AddArticle_OnClick(object sender, EventArgs e)
    {
        string text = this.Add_ShortTitle.Text;
        string text2 = this.Add_LongTitle.Text;
        int channelID = Convert.ToInt32(this.Add_Channel.SelectedValue.ToString());
        int titleType = 0;
        string text3 = this.Add_ArticleType.Text;
        string text4 = this.UploadPicUrl(this.Add_FileUpload);
        int isHighDisplay = Convert.ToInt32(this.Add_IsHighDisplay.SelectedValue);
        int classID = Convert.ToInt32(this.Add_ClassID.SelectedValue);
        int specialID = Convert.ToInt32(this.Add_SpecialID.SelectedValue);
        int linkType = Convert.ToInt32(this.Add_LinkType.SelectedValue);
        string text5 = this.Add_LinkUrl.Text;
        int status = Convert.ToInt32(this.Add_Status.SelectedValue);
        int onTop = this.Add_IsTop.Checked ? 1 : 0;
        int elite = this.Add_IsElite.Checked ? 1 : 0;
        string text6 = this.Add_Editor.Text;
        int hits = Convert.ToInt32(this.Add_Hits.Text);
        DateTime inputTime = Convert.ToDateTime(this.Add_InputTime.Text);
        int validay = Convert.ToInt32(this.Add_Validay.Text);
        string text7 = this.Add_TemplatePath.Text;
        string text8 = this.txtContent.Text;
        int titleLength = 0;
        bool flag = this.m_Article.AddNewArticle(channelID, classID, text3, titleType, text, text2, text8, text5, titleLength, text4, inputTime, hits, text6, onTop, elite, validay, status, specialID, text4, isHighDisplay, linkType, text7);
        if (flag)
        {
            this.WriteErrMsg("", Language.GetAdminMsg("Common/AddSuccess"), "Admin_ShopIndexArticle.aspx");
        }
        else
        {
            this.WriteErrMsg("", Language.GetAdminMsg("Common/EditFail", "未知"), "");
        }
    }
    protected void EditArticle_OnClick(object sender, EventArgs e)
    {
        if (this.m_ArticleID == 0)
        {
            this.WriteErrMsg("", Language.GetAdminMsg("Common/ParamErr"), "");
        }
        string text = this.Edit_ShortTitle.Text;
        string text2 = this.Edit_LongTitle.Text;
        int channelID = Convert.ToInt32(this.Edit_Channel.SelectedValue.ToString());
        int titleType = 0;
        string text3 = this.Edit_ArticleType.Text;
        string text4 = this.UploadPicUrl(this.Edit_FileUpload);
        int isHighDisplay = Convert.ToInt32(this.Edit_IsHighDisplay.SelectedValue);
        int classID = Convert.ToInt32(this.Edit_ClassID.SelectedValue);
        int specialID = Convert.ToInt32(this.Edit_SpecialID.SelectedValue);
        int linkType = Convert.ToInt32(this.Edit_LinkType.SelectedValue);
        string text5 = this.Edit_LinkUrl.Text;
        int status = Convert.ToInt32(this.Edit_Status.SelectedValue);
        int onTop = this.Edit_IsTop.Checked ? 1 : 0;
        int elite = this.Edit_IsElite.Checked ? 1 : 0;
        string text6 = this.Edit_Editor.Text;
        int hits = Convert.ToInt32(this.Edit_Hits.Text);
        DateTime updateTime = Convert.ToDateTime(this.Edit_InputTime.Text);
        int validay = Convert.ToInt32(this.Edit_Validay.Text);
        string text7 = this.Edit_TemplatePath.Text;
        string text8 = this.FCKeditor1.Text;
        int titleLength = 0;
        bool flag = this.m_Article.UpdateArticle(this.m_ArticleID, channelID, classID, text3, titleType, text, text2, text8, text5, titleLength, text4, hits, text6, onTop, elite, validay, status, specialID, updateTime, text4, isHighDisplay, linkType, text7);
        if (flag)
        {
            XmlDocument xmlDocument = new XmlDocument();
            string filename = base.Server.MapPath("~/") + "\\App_Data\\SiteConfig.config";
            xmlDocument.Load(filename);
            xmlDocument.SelectSingleNode("SiteConfigInfo/TemplateSetList/Article").InnerText = text7;
            xmlDocument.Save(filename);
            this.WriteErrMsg("", Language.GetAdminMsg("Common/EditSuccess"), "Admin_ShopIndexArticle.aspx");
        }
        else
        {
            this.WriteErrMsg("", Language.GetAdminMsg("Common/EditFail", "未知"), "");
        }
    }
    public string UploadPicUrl(FileUpload m_FileUpload)
    {
        string result;
        if (m_FileUpload.PostedFile.FileName.Length > 0)
        {
            string str = "/" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
            string text = string.Concat(new string[]
			{
				"/",
				DateTime.Now.Day.ToString(),
				DateTime.Now.Hour.ToString(),
				DateTime.Now.Minute.ToString(),
				DateTime.Now.Second.ToString()
			});
            if (m_FileUpload.PostedFile.FileName.LastIndexOf(".") < 1)
            {
                this.WriteErrMsg("", "上传文件格式错误！", "");
                result = string.Empty;
            }
            else
            {
                text += m_FileUpload.PostedFile.FileName.Substring(m_FileUpload.PostedFile.FileName.LastIndexOf("."));
                string text2 = this.InstallDir + "Shop/UploadFile/IndexArticle" + str + text;
                UpLoadFile upLoadFile = new UpLoadFile();
                upLoadFile.myfile = m_FileUpload;
                if (upLoadFile.UpLoad(text2))
                {
                    result = text2;
                }
                else
                {
                    this.WriteErrMsg("", upLoadFile.ErrString, "");
                    result = string.Empty;
                }
            }
        }
        else
        {
            result = string.Empty;
        }
        return result;
    }
}
